//
//  KRFindListViewController.m
//  KEREN
//
//  Created by MW on 2023/4/23.
//

#import "KRFindListViewController.h"
#import "KRFindCell.h"
#import "KRFindCarouselModel.h"
#import "FindDetailViewController.h"
#import "KRFindListModel.h"
@interface KRFindListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArrM;
@property (nonatomic, assign) int type;
@property (nonatomic, assign) int index;
@end

@implementation KRFindListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _index = 1;
    self.view.backgroundColor = [UIColor clearColor];
}
- (void)getFindVehicleSkillList:(int)type{
    [[KRAlartManager shareInstance] showLodingAlertView:@"努力加载中..."];
    WEAKSELF
    _type = type;
    NSDictionary *parameters;
    if(type == 0){
        parameters = @{@"index":@(_index),@"size":@(20)};
    }else{
        parameters = @{@"index":@(_index),@"size":@(20),@"kind":@(type)};
    }
    [NetworkingTool POST:Find_vehicleSkill_list parameters:parameters isDeviceUrl:YES success:^(id  _Nonnull response, NSURLSessionDataTask * _Nonnull operation) {
        [weakSelf.tableView.mj_footer endRefreshing];
        [weakSelf.tableView.mj_header endRefreshing];
        [[KRAlartManager shareInstance] hidenLodingImageView];

        if([response[@"code"] intValue] == 0){
            if(weakSelf.index == 1){
                [weakSelf.dataArrM removeAllObjects];
            }
            if([response[@"data"][@"list"] isKindOfClass:[NSArray class]]){
                NSArray *dataArr = response[@"data"][@"list"];
                for (int i = 0 ; i < dataArr.count; i ++) {
                    NSDictionary *dict = dataArr[i];
                    KRFindListModel *model = [KRFindListModel initWithDictionary:dict];
                    [weakSelf.dataArrM addObject:model];
                }
                if(dataArr.count < 20){
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                if (self.dataArrM.count == 0) {
                    [weakSelf.tableView.mj_footer setHidden:YES];
                }else{
                    [weakSelf.tableView.mj_footer setHidden:NO];
                }

            }
            if(weakSelf.dataArrM.count == 0){
                KRNodataView *view = [[KRNodataView alloc] initWithFrame:CGRectZero];
                weakSelf.tableView.tableHeaderView = view;
            }else{
                weakSelf.tableView.tableHeaderView = nil;
            }
            [weakSelf.tableView reloadData];
        }else{
            KRNodataView *view = [[KRNodataView alloc] initWithFrame:CGRectZero];
            weakSelf.tableView.tableHeaderView = view;
        }

    } failure:^(NSError * _Nonnull error, NSURLSessionDataTask * _Nonnull operation) {
        KRNodataView *view = [[KRNodataView alloc] initWithFrame:CGRectZero];
        weakSelf.tableView.tableHeaderView = view;
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor clearColor];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [UIView new];
    footView.backgroundColor = [UIColor clearColor];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50 + 20 + 20 + (DEVICE_WIDTH - 48) / 16 * 9 + 24;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KRFindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KRFindCell"];
    KRFindListModel *model = self.dataArrM[indexPath.section];
    [cell.userIcon sd_setImageWithURL:[NSURL URLWithString:model.head_img] placeholderImage:[UIImage imageNamed:@"user_default_icon"]];
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:model.url] placeholderImage:[UIImage imageNamed:@"find_big_icon"]];
    cell.titleLbl.text = model.title;
    cell.userName.text = model.nick_name;
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lineView.hidden = YES;
    if([[DeteManager shareInstance] getCurrentDay] - model.created_at < 3600){
        cell.timeLbl.text = [NSString stringWithFormat:@"%ld分钟前",([[DeteManager shareInstance] getCurrentDay] - model.created_at)/ 60];
    }else{
        cell.timeLbl.text = [[DeteManager shareInstance] timeFormatted:model.created_at];

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    KRFindListModel *model = self.dataArrM[indexPath.section];
    FindDetailViewController *vc = [[FindDetailViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}


- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT -  DEVICE_WIDTH/ 16 * 9 - 58 - 70)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.rowHeight = 280;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"KRFindCell" bundle:nil] forCellReuseIdentifier:@"KRFindCell"];
        [self.view addSubview:_tableView];
        WEAKSELF
        _tableView.mj_header =
        [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.index = 1;
            [self getFindVehicleSkillList:weakSelf.type];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.index ++;
            [self getFindVehicleSkillList:weakSelf.type];
        }];
        _tableView.mj_footer.hidden = YES;

    }
    return _tableView;
}

- (NSMutableArray *)dataArrM{
    if(!_dataArrM){
        _dataArrM = [NSMutableArray array];
    }
    return _dataArrM;
}


@end
